﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace DALL.SysManage
{
    /// <summary>
    /// 数据访问类:T_PUB_LIBRARY
    /// </summary>
    public class T_PUB_LIBRARY
    {
        public T_PUB_LIBRARY()
        { }
        #region  Method


        /// <summary>
        /// 获得仓库信息和所属店
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetLibraryInfo(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("select LIBRARY_ID,  \n");
            varname1.Append("       LIBRARY_NAME, \n");
            varname1.Append("       a.BRANCH_ID, \n");
            varname1.Append("       BRANCH_NAME, \n");
            varname1.Append("       a.HEAD \n");
            varname1.Append("from T_PUB_LIBRARY AS a, \n");
            varname1.Append("       T_PUB_BRANCH as b \n");
            varname1.Append("where b.BRANCH_ID = a.BRANCH_ID \n");


            if (strWhere.Trim() != "")
            {
                varname1.Append(" and " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());

        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int LIBRARY_ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_PUB_LIBRARY where LIBRARY_ID=@LIBRARY_ID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, LIBRARY_ID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_LIBRARY model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_LIBRARY(");
            strSql.Append("LIBRARY_NAME,BRANCH_ID,HEAD,IS_LIBRARY)");

            strSql.Append(" values (");
            strSql.Append("@LIBRARY_NAME,@BRANCH_ID,@HEAD,@IS_LIBRARY)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LIBRARY_NAME", DbType.String, model.LIBRARY_NAME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "HEAD", DbType.String, model.HEAD);
            db.AddInParameter(dbCommand, "IS_LIBRARY", DbType.Boolean, model.IS_LIBRARY);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_LIBRARY model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_LIBRARY set ");
            strSql.Append("LIBRARY_NAME=@LIBRARY_NAME,");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("HEAD=@HEAD,");
            strSql.Append("IS_LIBRARY=@IS_LIBRARY");
            strSql.Append(" where LIBRARY_ID=@LIBRARY_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "LIBRARY_NAME", DbType.String, model.LIBRARY_NAME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "HEAD", DbType.String, model.HEAD);
            db.AddInParameter(dbCommand, "IS_LIBRARY", DbType.Boolean, model.IS_LIBRARY);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int LIBRARY_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_LIBRARY ");
            strSql.Append(" where LIBRARY_ID=@LIBRARY_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, LIBRARY_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_LIBRARY GetModel(int LIBRARY_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LIBRARY_ID,LIBRARY_NAME,BRANCH_ID,HEAD,IS_LIBRARY from T_PUB_LIBRARY ");
            strSql.Append(" where LIBRARY_ID=@LIBRARY_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, LIBRARY_ID);
            Model.SysManage.T_PUB_LIBRARY model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public List<Model.SysManage.T_PUB_LIBRARY> GetModelByBranch_id(int BRANCH_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LIBRARY_ID,LIBRARY_NAME,BRANCH_ID,HEAD,IS_LIBRARY from T_PUB_LIBRARY ");
            strSql.Append(" where BRANCH_ID=@BRANCH_ID and IS_LIBRARY = 1");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, BRANCH_ID);
            List<Model.SysManage.T_PUB_LIBRARY> list = new List<Model.SysManage.T_PUB_LIBRARY>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LIBRARY_ID,LIBRARY_NAME,BRANCH_ID,HEAD,IS_LIBRARY ");
            strSql.Append(" FROM T_PUB_LIBRARY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }



        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_LIBRARY> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LIBRARY_ID,LIBRARY_NAME,BRANCH_ID,HEAD,IS_LIBRARY ");
            strSql.Append(" FROM T_PUB_LIBRARY ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_LIBRARY> list = new List<Model.SysManage.T_PUB_LIBRARY>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_LIBRARY ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_LIBRARY model = new Model.SysManage.T_PUB_LIBRARY();
            object ojb;
            ojb = dataReader["LIBRARY_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LIBRARY_ID = (int)ojb;
            }
            model.LIBRARY_NAME = dataReader["LIBRARY_NAME"].ToString();
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            model.HEAD = dataReader["HEAD"].ToString();
            ojb = dataReader["IS_LIBRARY"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IS_LIBRARY = (bool)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
